package lumingweihua.future.cn.lumingweihua.ui.order;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Intents;
import com.zhusx.core.utils._Lists;
import com.zhusx.core.utils._Strings;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.RushDetailData;

/**
 * Created by z1986 on 2018/3/16.
 */

public class RushOrderDetailActivity extends BaseCompatActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_rough_weight)
    TextView tvRoughWeight;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_fromName)
    TextView tvFromName;
    @BindView(R.id.tv_fromCity)
    TextView tvFromCity;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_weight)
    TextView tvWight;
//    @BindView(R.id.tv_client_price)
//    TextView tvClientPrice;
    @BindView(R.id.tv_service_price)
    TextView tvServicePrice;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_note)
    TextView tvNote;
    @BindView(R.id.tv_create_date)
    TextView tvCreateDate;
    @BindView(R.id.layout_content)
    ScrollView layoutContent;
    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.layout_upload)
    LinearLayout layoutUpload;
    @BindView(R.id.tv_mileage)
    TextView tvMileage;

    RushDetailData data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_rush_detail);
        ButterKnife.bind(this);
        String id = getIntent().getStringExtra(_EXTRA_String_ID);

        LoadData<RushDetailData> loadData = new LoadData<>(LoadData.Api.微信端抢单详情, this);
        loadData._setOnLoadingListener(new LoadingHelper<RushDetailData>(layoutContent, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<RushDetailData> data) {
                init(data.getData());
            }
        });
        loadData._loadData(id);
    }

    private void init(RushDetailData en) {
        this.data = en;
        switch (String.valueOf(en.indent_status)) {
            case "1"://待审核
                ivImage.setImageResource(R.drawable.iv_shenhe);
                tvMessage.setTextColor(getResources().getColor(R.color.font_blue));
                tvMessage.setText("认证信息审核中.请耐心等待");
                break;
            case "2"://已通过
                ivImage.setImageResource(R.drawable.iv_tongguo);
                tvMessage.setTextColor(Color.parseColor("#59b469"));
                tvMessage.setText("抢单信息已通过\n请提醒司机登录鲁明危运司机端\n及时完成货物运输");
                break;
            case "3"://已拒绝
                ivImage.setImageResource(R.drawable.iv_shibai);
                tvMessage.setTextColor(getResources().getColor(R.color.red));
                tvMessage.setText(data.reason);
                break;
        }
        tvName.setText(en.car_name);
        tvRoughWeight.setText(en.rough_weight + "吨");
        tvPhone.setText(en.car_mobile);
        tvNum.setText(en.good_num);
        tvFromName.setText(en.loading);
        tvFromCity.setText(en.loading_address);
        tvDate.setText(en.use_data + " " + _Strings.valueOf(en.use_time));
        tvType.setText(en.type);
        tvMileage.setText(en.mileage);
        tvWight.setText(en.weight+"吨");
        tvPrice.setText(en.transportation + "元");
        //tvClientPrice.setText("货主报价: " + en.good_price + "/吨");
        tvServicePrice.setText(en.terrace_price + "元");
        tvCreateDate.setText(en.create_time);
        if (!_Lists.isEmpty(en.unload)) {
            for (int i = 0; i < en.unload.size(); i++) {
                View itemView = LayoutInflater.from(this).inflate(R.layout.layout_item_order, layoutUpload, true);
                ((TextView) itemView.findViewById(R.id.tv_toCity)).setText(en.unload_address.get(i));
                ((TextView) itemView.findViewById(R.id.tv_toName)).setText(en.unload.get(i));
                ((TextView) itemView.findViewById(R.id.tv_toCity)).setTextColor(Color.parseColor("#333333"));
            }
        }
        tvNote.setText(en.remark);
    }

    @OnClick(R.id.tv_phone)
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.tv_phone:
                if (data != null && !TextUtils.isEmpty(data.car_mobile)) {
                    new AlertDialog.Builder(this)
                            .setMessage("是否拨打电话给司机?")
                            .setNegativeButton("拨打", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    _Intents.dialPhone(RushOrderDetailActivity.this, data.car_mobile);
                                }
                            })
                            .setPositiveButton("取消", null)
                            .create()
                            .show();
                }
                break;
        }
    }
}
